﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using Benefit.Models.Sys;
namespace Benefit.Models.Month
{
    /// <summary>
    /// 账户资金情况
    /// </summary>
    public class AccountMonthMoney
    {
        /// <summary>
        /// 编号
        /// </summary>
        public int Id { get; set; }
        /// <summary>
        /// 账户
        /// </summary>
        public int AccountId { get; set; }
        /// <summary>
        /// 账户
        /// </summary>
        public virtual Account Account { get; set; }
        /// <summary>
        /// 多仓持仓
        /// </summary>
        public int LongPosition { get; set; }
        /// <summary>
        /// 空仓持仓
        /// </summary>
        public int ShortPosition { get; set; }
        /// <summary>
        /// 期初结存
        /// </summary>
        public double PresetTlePosit { get; set; }
        /// <summary>
        /// 期末结存
        /// </summary>
        public double SettlePosit { get; set; }
        /// <summary>
        /// 冻结保证金
        /// </summary>
        public double FrozenMargin { get; set; }
        /// <summary>
        /// 冻结手续费
        /// </summary>
        public double FrozenCommission { get; set; }
        /// <summary>
        /// 入金金额
        /// </summary>
        public double Deposit { get; set; }
        /// <summary>
        /// 出金金额
        /// </summary>
        public double WithDraw { get; set; }
        /// <summary>
        /// 交割保证金
        /// </summary>
        public double DeliveryMargin { get; set; }
        /// <summary>
        /// 交割手续费
        /// </summary>
        public double DeliveryCommission { get; set; }
        /// <summary>
        /// 追加保证金
        /// </summary>
        public double AppendMargin { get; set; }
        /// <summary>
        /// 基础保证金
        /// </summary>
        public double BaseMargin { get; set; }
        /// <summary>
        /// 质押金额
        /// </summary>
        public double Mortgage { get; set; }
        /// <summary>
        /// 上次质押金额
        /// </summary>
        public double Premortgage { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Commission { get; set; }
        /// <summary>
        /// 可用资金
        /// </summary>
        public double AvailableMoney { get; set; }
        /// <summary>
        /// 可取资金
        /// </summary>
        public double WithDrawQuota { get; set; }
        /// <summary>
        /// 动态权益
        /// </summary>
        public double DynamicMoney { get; set; }
        /// <summary>
        /// 占用保证金
        /// </summary>
        public double Margin { get; set; }
        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double CloseProfit { get; set; }
        /// <summary>
        /// 持仓盈亏
        /// </summary>
        public double PositionProfit { get; set; }
        /// <summary>
        /// 风险度
        /// </summary>
        public double RiskRate { get; set; }
        /// <summary>
        /// 交易年
        /// </summary>
        public int Year { get; set; }
        /// <summary>
        /// 交易月
        /// </summary>
        public int Month { get; set; }

        ///// <summary>
        ///// 删除当月的记录
        ///// </summary>
        ///// <param name="month"></param>
        ///// <param name="year"></param>
        //public void Delete(int month, int year, DB.DBManager db)
        //{
        //    var query = from t in db.AccountMonthMoney where t.Month == month && t.Year == year select t;
        //    foreach (Data.Month.AccountMonthMoney c in query)
        //    {
        //        db.AccountMonthMoney.Remove(c);
        //    }
        //    db.SaveChanges();
        //}

        ///// <summary>
        ///// 初始化账户月资金信息
        ///// </summary>
        ///// <param name="month"></param>
        ///// <param name="year"></param>
        //public void InitAccountMonthMoney(int month, int year, DB.DBManager db)
        //{
        //    var query = db.Accounts.ToList();
        //    foreach (Sys.Account account in query)
        //    {
        //        AccountMonthMoney change = new AccountMonthMoney();
        //        Data.Day.AccountDayMoney lastDayMoney = new Day.AccountDayMoney();
        //        Data.Day.AccountDayMoney fristDayMoney = new Day.AccountDayMoney();
        //        change.AccountId = account.Id;
        //        var _query = (from t in db.AccountDayMoney where t.AccountId == account.Id && t.TradeHistory.MPdate == month && t.TradeHistory.YPdate == year orderby t.Id descending select t).ToList();
        //        if (_query.Count() > 0)
        //        {
        //            lastDayMoney = _query.First();
        //            fristDayMoney = _query.LastOrDefault();
        //        }
        //        ("账户" + account.Id + "日记录条数:" + _query.Count()).Debug();
        //        var existQuery = from t in db.AccountMonthMoney where t.Month == month && t.Year == year && t.AccountId == account.Id select t;
        //        if (existQuery.Count() > 0)
        //        {
        //            change = existQuery.First();
        //        }

        //        change.AppendMargin = lastDayMoney.AppendMargin;
        //        change.AvailableMoney = lastDayMoney.AvailableMoney;
        //        change.BaseMargin = lastDayMoney.BaseMargin;
        //        change.CloseProfit = _query.Sum(a => a.CloseProfit);
        //        change.Commission = _query.Sum(a => a.Commission);
        //        change.DeliveryCommission = _query.Sum(a => a.DeliveryCommission);
        //        change.DeliveryMargin = lastDayMoney.DeliveryMargin;
        //        change.Deposit = _query.Sum(a => a.Deposit);
        //        change.DynamicMoney = lastDayMoney.DynamicMoney;
        //        change.FrozenCommission = lastDayMoney.FrozenCommission;
        //        change.FrozenMargin = lastDayMoney.FrozenMargin;
        //        change.LongPosition = lastDayMoney.LongPosition;
        //        change.Margin = lastDayMoney.Margin;
        //        change.Month = month;
        //        change.Mortgage = lastDayMoney.Mortgage;
        //        change.PositionProfit = lastDayMoney.PositionProfit;
        //        change.Premortgage = fristDayMoney.Premortgage;
        //        change.PresetTlePosit = fristDayMoney.PresetTlePosit;
        //        change.RiskRate = lastDayMoney.RiskRate;
        //        change.SettlePosit = lastDayMoney.SettlePosit;
        //        change.ShortPosition = lastDayMoney.ShortPosition;
        //        change.WithDraw = _query.Sum(a => a.WithDraw);
        //        change.WithDrawQuota = lastDayMoney.WithDrawQuota;
        //        change.Year = year;
        //        if (existQuery.Count() == 0)
        //        {
        //            db.AccountMonthMoney.Add(change);
        //        }
        //    }
        //    db.SaveChanges();
        //}
        ///// <summary>
        ///// 获取账号和交易员月资金信息
        ///// </summary>
        ///// <param name="operatorid"></param>
        ///// <param name="month"></param>
        ///// <param name="year"></param>
        ///// <param name="db"></param>
        ///// <returns></returns>
        //public List<AccountMonthMoney> GetAccountAndAllChange(int operatorid, int month, int year, DB.DBManager db)
        //{
        //    List<AccountMonthMoney> list = new List<AccountMonthMoney>();
        //    Sys.TradeHistory th = new Sys.TradeHistory();
        //    th = th.GetMonthLastTradingDay(year, month, db);
        //    List<Sys.Account> accounts = new Sys.DayOperatorAccount().GetDayOperatorAccounts(th.GetDateTimeFromTradeHistoryId(th.Id, db), operatorid, db);
        //    if (accounts.Count > 0)
        //    {
        //        foreach (Sys.Account account in accounts)
        //        {
        //            bool hasChangedOwner = account.HasChangedOwnerInMonth(year, month, account.Id);
        //            //  if (hasChangedOwner)//换过用户，那么从每天的交易情况累计总值
        //            //   {
        //            BenefitHelper.Data.Month.AccountMonthMoney change = db.AccountMonthMoney.Where(a => a.AccountId == account.Id).Where(a => a.Month == month).Where(a => a.Year == year).First();
        //            BenefitHelper.Data.Month.AccountMonthMoney sum = new BenefitHelper.Data.Month.AccountMonthMoney();
        //            sum.PresetTlePosit = change.PresetTlePosit;
        //            sum.SettlePosit = change.SettlePosit;
        //            var chu = db.CashIOHistorys.Where(a => a.AccountId == account.Id).Where(a => a.Pdate.Year == year).Where(a => a.Pdate.Month == month).Where(a => a.Force.Equals("转出")).ToList();
        //            if (chu.Count > 0)
        //            {
        //                sum.WithDraw = chu.Sum(a => a.Amount);
        //            }
        //            else
        //            {
        //                sum.WithDraw = 0;
        //            }
        //            var ru = db.CashIOHistorys.Where(a => a.AccountId == account.Id).Where(a => a.Pdate.Year == year).Where(a => a.Pdate.Month == month).Where(a => a.Force.Equals("转入")).ToList();
        //            if (ru.Count > 0)
        //            {
        //                sum.Deposit = ru.Sum(a => a.Amount);
        //            }
        //            else
        //            {
        //                sum.Deposit = 0;
        //            }
        //            sum.AccountId = account.Id;
        //            sum.Account = account;
        //            list.Add(sum);
        //            //    }
        //        }
        //    }
        //    var demo = new BenefitHelper.Data.Month.AccountMonthMoney();
        //    demo.AccountId = 0;
        //    demo.PresetTlePosit = list.Sum(a => a.PresetTlePosit);
        //    demo.SettlePosit = list.Sum(a => a.SettlePosit);
        //    demo.WithDraw = list.Sum(a => a.WithDraw);
        //    demo.Deposit = list.Sum(a => a.Deposit);
        //    list.Add(demo);
        //    return list;
        //}
    }
}